PROJECT(SkeletonCppGame C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

# -- CHECK BASIC SYSTEM -- #
INCLUDE(CheckFunctionExists)
#CHECK_FUNCTION_EXISTS(sqrt HAVE_SQRT)

# -- SET OPTIONS -- #
SET(APP_NAME "\"Skeleton C++ game\"")
SET(VERSION_MAJOR 0)
SET(VERSION_MINOR 1)
SET(VERSION_PATCH 0)
SET(APP_DESCRIPTION "\"Skeleton for C++ games\"")
SET(BUILD_SHARED_LIBS OFF)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
SET(APP_SHORT "skeleton-cpp-game")

OPTION(USE_GLFW "Use GLFW as window/input library" OFF)
OPTION(USE_OGRE "Use Ogre3D as rendering engine" OFF)
OPTION(USE_CEGUI "Use CEGUI as GUI library" OFF)
OPTION(USE_OIS "Use OIS as input library" OFF)
OPTION(USE_GLEW "Use GLEW as OpenGL extension manager" OFF)
OPTION(USE_SFML "Use SFML as window/network/audio/input library" OFF)
OPTION(USE_CHAISCRIPT "Use ChaiScript as script language" OFF)
OPTION(USE_SQUIRREL "Use Squirrel as script language" OFF)
OPTION(USE_BOOST "Use Boost as common library" OFF)
OPTION(USE_BULLET "Use Bullet as physics engine" OFF)
OPTION(USE_BOX2D "Use Box2D as physics engine" OFF)
OPTION(USE_PORTAUDIO "Use PortAudio as audio output" OFF)
OPTION(USE_VORBIS "Use libogg and libvorbis for audio" OFF)

# -- COMPILING OPTIONS -- #
IF(UNIX)
	LIST(APPEND CMAKE_CXX_FLAGS "-std=c++11")
ENDIF()

SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) 
SET(CMAKE_INSTALL_RPATH "$ORIGIN")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# -- CONFIGURE FILE -- #
CONFIGURE_FILE(
	${PROJECT_SOURCE_DIR}/src/config.hpp.in
	${PROJECT_BINARY_DIR}/src/config.hpp
)
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR}/src)

# -- LIBS -- #
IF(USE_GLFW)
	ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/libs/glfw)
	INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/libs/glfw/include)
	SET(GAME_LIBS ${GAME_LIBS} ${GLFW_LIBRARIES})
	SET(GAME_DEPENDS ${GAME_DEPENDS} glfw)
ENDIF()

IF(USE_OGRE)
	FIND_PACKAGE(OGRE)
	INCLUDE_DIRECTORIES(${OGRE_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${OGRE_LIBRARIES})
ENDIF()

IF(USE_OIS)
	FIND_PACKAGE(OIS)
	INCLUDE_DIRECTORIES(${OIS_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${OIS_LIBRARIES})
ENDIF()

IF(USE_CEGUI)
	FIND_PACKAGE(CEGUI)
	INCLUDE_DIRECTORIES(${CEGUI_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${CEGUI_LIBRARIES})
	IF(USE_OGRE)
		LIST(APPEND GAME_LIBS ${CEGUI_OgreRenderer_LIBRARIES})
	ENDIF()
ENDIF()

IF(USE_GLEW)
	FIND_PACKAGE(GLEW)
	INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${GLEW_LIBRARIES})
ENDIF()

IF(USE_SFML)
	SET(SFML_STATIC_LIBRARIES TRUE)
	FIND_PACKAGE(SFML)
	INCLUDE_DIRECTORIES(${SFML_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${SFML_LIBRARIES})
ENDIF()

IF(USE_BOOST)
	FIND_PACKAGE(Boost) # FIND_PACKAGE(BOOST COMPONENTS system locale)
	INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${Boost_LIBRARIES})
ENDIF()

IF(USE_BULLET)
	FIND_PACKAGE(Bullet)
	INCLUDE_DIRECTORIES(${Bullet_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${Bullet_LIBRARIES})
ENDIF()

IF(USE_PORTAUDIO)
	FIND_PACKAGE(Portaudio)
	INCLUDE_DIRECTORIES(${Portaudio_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${Portaudio_LIBRARIES})
ENDIF()

IF(USE_VORBIS)
	FIND_PACKAGE(Ogg)
	FIND_PACKAGE(Vorbis)
	INCLUDE_DIRECTORIES(${Ogg_INCLUDE_DIR})
	INCLUDE_DIRECTORIES(${Vorbis_INCLUDE_DIR})
	LIST(APPEND GAME_LIBS ${Ogg_LIBRARIES})
	LIST(APPEND GAME_LIBS ${Vorbis_LIBRARIES})
ENDIF()

#IF(USE_SQUIRREL)
#	FIND_PACKAGE(Squirrel)
#	INCLUDE_DIRECTORIES(${Squirrel_INCLUDE_DIR})
#	LIST(APPEND GAME_LIBS ${Squirrel_LIBRARIES})
#ENDIF()

# -- SOURCE FILES -- #
FILE(GLOB GAME_SRC "${PROJECT_SOURCE_DIR}/src/*.cpp")

# -- ADD EXECUTABLE -- #
IF(MSVC)
	LIST(APPEND GAME_SRC "res/win32.rc")
ENDIF()

ADD_EXECUTABLE(SkeletonCppGame ${GAME_SRC})
IF(USE_GLFW)
	ADD_DEPENDENCIES(SkeletonCppGame ${GAME_DEPENDS})
ENDIF()
TARGET_LINK_LIBRARIES(SkeletonCppGame ${GAME_LIBS})

# -- INSTALLATION -- #
INCLUDE(InstallRequiredSystemLibraries)
#INSTALL() install the targets
#COPY RESOURCES AND EXECUTABLE
#ADD DEPENDENCIES
#Use ldd script
#Add launcher bash script
#Add desktop file configuration
#Generate http://codevarium.gameka.com.br/deploying-cc-linux-applications-supporting-both-32-bit-and-64-bit/

# -- BUILD DOCS -- #
CONFIGURE_FILE(
	${PROJECT_SOURCE_DIR}/Doxyfile.in
	${PROJECT_BINARY_DIR}/Doxyfile
)
ADD_CUSTOM_TARGET(doxygen doxygen ${PROJECT_BINARY_DIR}/Doxyfile DEPENDS SkeletonCppGame WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMENT "Generating documentation" VERBATIM )
ADD_CUSTOM_TARGET(man ronn -r ${PROJECT_SOURCE_DIR}/README.md WORKING_DIRECTORY ${PROJECT_BINARY_DIR} COMMENT "Generating MAN pages")

# -- INSTALLATION SCRIPTS -- #
ADD_CUSTOM_TARGET(installUbuntu sudo ${PROJECT_SOURCE_DIR}/tools/install-ubuntu.sh COMMENT "Installing dependencies for Ubuntu" VERBATIM)
ADD_CUSTOM_TARGET(installOpenSUSE sudo ${PROJECT_SOURCE_DIR}/tools/install-opensuse.sh COMMENT "Installing dependencies for openSUSE" VERBATIM)
ADD_CUSTOM_TARGET(installFedora sudo ${PROJECT_SOURCE_DIR}/tools/install-fedora.sh COMMENT "Installing dependencies for Fedora" VERBATIM)
ADD_CUSTOM_TARGET(installArch sudo ${PROJECT_SOURCE_DIR}/tools/install-arch.sh COMMENT "Installing dependencies for Arch" VERBATIM)
ADD_CUSTOM_TARGET(installHaiku ${PROJECT_SOURCE_DIR}/tools/install-haiku.sh COMMENT "Installing dependencies for Haiku" VERBATIM)
ADD_CUSTOM_TARGET(installWindows ${PROJECT_SOURCE_DIR}/tools/install-windows.sh COMMENT "Installing dependencies for Windows" VERBATIM)
ADD_CUSTOM_TARGET(installNetBSD sudo ${PROJECT_SOURCE_DIR}/tools/install-netbsd.sh COMMENT "Installing dependencies for NetBSD" VERBATIM)

# -- ICONS -- #
ADD_CUSTOM_TARGET(icons ${PROJECT_SOURCE_DIR}/tools/generate-icons.sh ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} COMMENT "Generating icons" VERBATIM)
ADD_CUSTOM_TARGET(favicon ${PROJECT_SOURCE_DIR}/tools/generate-favicon.sh ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} DEPENDS icons COMMENT "Generating favicon" VERBATIM)


# -- TRANSLATIONS -- #
ADD_CUSTOM_TARGET(locales ${PROJECT_SOURCE_DIR}/tools/generate-locales.sh ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} ${APP_SHORT} COMMENT "Generating locales" VERBATIM)
